import sys,math

def error_function(value):
	if(value < 0.1):
		return value
	if(value < 0.5):
		return (10 * value)
	if(value  < 0.9):
		return (100 * value)
	if(value  < 1):
		return (1000 * value)
	if(value  < 1.1):
		return (10000 * value)
	if(value  <= 2):
		return (100000 * value)

def error_function_2(value):
	if(value < 1):
		return 0
	else:
		return 1
        
if(sys.argv.__len__()>1):
	
	## OPEN FILE AND READ FILE
	file_name = sys.argv[1]
	file_stream = open(file_name)
	lines = file_stream.readlines()
	lines2 = lines
	file_stream.close()
	
	## GET CLOCK 0 VALUES
	clock_0_values = [float(x) for x in lines[0].split(":")[1].split()]
	
	## GET LOWEST VALUE
	lowest_value = min(clock_0_values)
	
	## ADJUST START INDEX 
	index = 0
	
	while(clock_0_values[index] == lowest_value):
		index +=1
	while(clock_0_values[index] != lowest_value):
		index +=1
	
	
	## GET OUTPUT SAMPLES AND CLOCK
	output_values = list
	output_clock_values = list()
	output_clock = 0
	
	for line in lines:
		if(line.split(":")[0] == "CLOCK OUTPUT#0"):
			output_clock = line.split(":")[1].rstrip("\n")
			
	for line in lines:
		if(line.split(":")[0] == ("CLOCK "+output_clock).rstrip("\n")):
			output_clock_values = [float(x) for x in line.split(":")[1].split()]
			
	for line in lines:
		if(line.split(":")[0] == ("OUTPUT#"+lines[-1].split(":")[0].split("#")[1])):
			output_values = [float(x) for x in line.split(":")[1].split()]
		
	
	
	## ADJUST START INDEX IF CLOCK NOT 0
	lowest_value = min(output_clock_values)
	if(int(output_clock) != 0):
		while(output_clock_values[index] == lowest_value):
			index = index+1
		while(output_clock_values[index] != lowest_value):
			index = index+1
			
	#print output_clock_values
	#index = index+1
	
	
	## READ INSTANCE 
	instance_file_name = sys.argv[2]
	file_stream = open(instance_file_name)
	lines = file_stream.readlines()
	file_stream.close()
	
	## READ ENTRY TABLE
	entry_table = lines[lines.index("#ENTRY_TEST\n")+1:]
	entry_table = [i.rstrip("\n").rstrip("\t") for i in entry_table]
	entry_table = [[float(j) for j in i.split()] for i in entry_table]
	
	## READ TABLE TRUTH
	truth_table = lines[lines.index("#TRUTHTABLE\n")+1:lines.index("#ENTRY_TEST\n")]
	truth_table = [i.rstrip("\n").rstrip("\t") for i in truth_table]
	truth_table = [[float(j) for j in i.split()] for i in truth_table]
	output_truth_table = [i[-1] for i in truth_table]
	
	
	#print output_clock_values[index-2],output_clock_values[index-1],output_clock_values[index],output_clock_values[index+1],output_clock_values[index+2]
	
	## CALCULATE ERROR
	entry_num =0
	error_list = list()
	for entry_num in entry_table:
		error = 0
		error_count = 0;
		while(output_clock_values[index] == lowest_value):
			error += error_function_2(math.fabs(entry_num[-1]-output_values[index]))
			#print entry_num[-1],error_function(math.fabs(entry_num[-1]-output_values[index])),output_values[index],index
			error_count +=1
			index+=1
		error_list.append(error/error_count)
		while(output_clock_values[index] != lowest_value):
			index+=1
		
	error_sum = 0
	i = 0
	for entry_num in entry_table:
		error_sum += error_list[i]/output_truth_table.count(entry_num[-1])
		i +=1
	print error_sum
	
